package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.utils.page.PageBean;
import com.wf.course.model.ad.CardCourseRelation;
import com.wf.course.model.ad.VipCard;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.service.ICardCourseRelationService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IVipCardService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 *  课程年卡Controller
 */
@RequestMapping("cardCourseRelation")
@Controller
public class CardCourseRelationController {

	@Autowired
	private ICardCourseRelationService cardCourseRelationService;
	@Autowired
	private ICourseInfoService courseInfoService;
	@Autowired
	private IVipCardService cardService;
	

	/**
	 * 列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param adTitle
	 * @param adStatus
	 * @param model
	 * @return
	 */
	@LogInfo("年卡-课程关系列表查询")
	@RequestMapping("cardCourseRelationList")
	public String cardCourseRelationList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "cardName", required = false) String cardName, 
			@RequestParam(value = "courseName", required = false) String courseName, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("cardName", cardName);
		cons.put("courseName", courseName);
		PageBean<CardCourseRelation> pageBean = new PageBean<CardCourseRelation>(pageNo, pageSize, null, cons);
		pageBean.setUrl("cardCourseRelation/cardCourseRelationList");
		this.cardCourseRelationService.indexInfo(pageBean);
		
		model.addAttribute("page", pageBean);
		return "ad/cardCourseRelationList";
	}

	/**
	 *  修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增和修改年卡课程，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String pageLoad(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			CardCourseRelation cardCourseRelation = cardCourseRelationService.queryCardCourseRelationById(id);
			model.addAttribute("cardCourseRelation", cardCourseRelation);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		List<CourseInfo> courses = courseInfoService.queryCourses();
		model.addAttribute("courses", courses);
		 
		List<VipCard> cards =  cardService.queryVipCards();
		model.addAttribute("cards", cards);

		return "ad/cardCourseRelationEdit";
	}

	/**
	 * 新增和修改
	 * 
	 * @param cardCourseRelation
	 * @param request
	 * @return
	 */
	@LogInfo("新增和修改年卡中的课程")
	@RequestMapping(value = "cardCourseRelationEdit" , produces = "text/html;charset=UTF-8")
	public String cardCourseRelationEdit(@ModelAttribute CardCourseRelation cardCourseRelation, HttpServletRequest request) {
		
		cardCourseRelationService.saveOrUpdateCardCourseRelation(cardCourseRelation);
		
		return "redirect:/cardCourseRelation/cardCourseRelationList";
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除年卡中的课程")
	@RequestMapping(value = "remove/{id}")
	public String removeCardCourseRelation(@PathVariable("id") Integer id) {
		
		cardCourseRelationService.deleteById(id);
		
		return "redirect:/cardCourseRelation/cardCourseRelationList";
	}
	

}
